package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TAdminDTO;
import com.dk.pojo.Comment;
import com.dk.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin  // 解决跨域
@RestController
@RequestMapping("commentController")
@Api(value = "留言文档信息")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "留言增加")
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message",value = "留言内容",required = true,paramType = "query"),
            @ApiImplicitParam(name = "token",value = "token值",required = true,paramType = "query"),
            @ApiImplicitParam(name = "carId",value = "车的id",required = true,paramType = "query")
    })
    public Result saveMessage(@RequestParam("message") String message,@RequestParam("token")String token,@RequestParam("carId")Long carId){
        TAdminDTO tAdminDTO = (TAdminDTO)redisTemplate.boundValueOps(token).get();
        Comment comment = new Comment();
        comment.setUserId(tAdminDTO.getUsername());
        comment.setMessage(message);
        comment.setCarId(carId);
        comment.setDate(new Date());
        //添加留言
        Comment comment1 = commentService.saveMassage(comment);
        return  Result.bulid(ConstantEnum.SUCCESS).setResult(comment1);
    }

    @ApiOperation(value = "展示留言")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carId",value = "车的id",required = true,paramType = "query")
    })
    public Result getMessage(@RequestParam("carId")Long carId){
        List<Comment> list = commentService.getMessage(carId);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }
}
